<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin_m
 *
 * @author QUYET
 */
class admin_m extends CI_Model{
    var $language = 1;
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->database();
        if($this->session->userdata('lang')=='en') $this->language = 2;
    }
    
    function get_all_tickets($cat_array,$per_page=10,$off_set=0,$lang=2){
        $this->db->select('ots_ticket.id,ots_ticket.expiry_date,ots_ticket.price,ots_ticket.img_id,ots_image.img_name,ots_ticket.quantity,ots_ticket.cat_id,ots_ticket_lang.tag,ots_ticket_lang.ticket_name,,ots_ticket_cat_lang.category');
        $this->db->from('ots_ticket');
        $this->db->join('ots_ticket_lang','ots_ticket.id = ots_ticket_lang.id_ticket','inner');
        $this->db->join('ots_ticket_cat','ots_ticket.cat_id = ots_ticket_cat.id','inner');
        $this->db->join('ots_ticket_cat_lang','ots_ticket_cat_lang.id_category = ots_ticket_cat.id','inner');
        $this->db->join('image', 'image.id=ticket.img_id', 'left');        
        $this->db->where_in('ots_ticket.cat_id',$cat_array);
        $this->db->where('ots_ticket_lang.id_lang',$this->language);
        $this->db->group_by('ots_ticket.id');
        $this->db->order_by('ots_ticket.id','DESC');
        $this->db->limit($per_page,$off_set);
        return $this->db->get()->result_array();
    }
    
    function count_all_ticket($cat_array){
        $this->db->select('ots_ticket.id,ots_ticket_lang.ticket_name,ots_ticket.price,ots_ticket.quantity,ots_image.img_name,ots_ticket_cat_lang.category');
        $this->db->from('ots_ticket');
        $this->db->join('ots_ticket_lang','ots_ticket.id = ots_ticket_lang.id_ticket','inner');
        $this->db->join('ots_ticket_cat','ots_ticket.cat_id = ots_ticket_cat.id','inner');
        $this->db->join('ots_ticket_cat_lang','ots_ticket_cat_lang.id_category = ots_ticket_cat.id','inner');
        $this->db->join('image', 'image.id=ticket.img_id', 'left');
        $this->db->where('ots_ticket_lang.id_lang = '.$this->language);
        $this->db->where_in('ots_ticket.cat_id',$cat_array);
        $this->db->group_by('ots_ticket.id');
        return count($this->db->get()->result_array());
    }
    
    function add_ticket($data){
        $record_update_main = array();
        if(isset($data['expiry_date'])) {
            $record_update_main['expiry_date'] = $data['expiry_date'];
            unset($data['expiry_date']);
        }
        if(isset($data['price'])) {
            $record_update_main['price'] = $data['price'];
            unset($data['price']);
        } 
        if(isset($data['img_id'])) {
            $record_update_main['img_id'] = $data['img_id'];
            unset($data['img_id']);
        }
        if(isset($data['quantity'])) {
            $record_update_main['quantity'] = $data['quantity'];
            unset($data['quantity']);
        }
        if(isset($data['cat_id'])) {
            $record_update_main['cat_id'] = $data['cat_id'];
            unset($data['cat_id']);
        }
        if(empty($record_update_main))  return false;
        $this->db->insert('ticket',$record_update_main);        
        $id_ticket = $this->db->insert_id();
        $data['id_ticket'] = $id_ticket;
        $data['id_lang'] = $this->language;
        return $this->db->insert('ticket_lang',$data)>0;
    }
    
    function get_details($id_ticket,$uselang = false){
        $this->db->select('ots_ticket.id,ots_ticket_lang.ticket_name,ots_ticket_lang.id_lang,ots_ticket_lang.description,ots_ticket_lang.meta_description,,ots_ticket_lang.meta_title,ots_ticket_lang.tag,ots_ticket_lang.meta_keyword,ots_ticket.expiry_date,ots_ticket.price,ots_ticket.img_id,ots_ticket.quantity,ots_ticket.cat_id,ots_image.img_name');
        $this->db->from('ots_ticket');
        $this->db->join('ots_ticket_lang','ots_ticket.id = ots_ticket_lang.id_ticket','inner');
        $this->db->join('image', 'image.id=ticket.img_id', 'left');
        $this->db->where('ots_ticket.id',$id_ticket);
        
        if($uselang) {
            $this->db->where('ots_ticket_lang.id_lang',$this->language);
            return $this->db->get()->row();
        }
        else{
            $one_cate_alldetail = $this->db->get()->result();
            if(count($one_cate_alldetail)==2){
                foreach($one_cate_alldetail as $catlg){
                    if($catlg->id_lang==$this->language) return $catlg;
                }
            }
            elseif(!empty($one_cate_alldetail[0])) return $one_cate_alldetail[0];
            else return false;
        }
    }
    
    function update_ticket($data,$id_ticket){
        $record_update_main = array();
        if(isset($data['expiry_date'])) {
            $record_update_main['expiry_date'] = $data['expiry_date'];
            unset($data['expiry_date']);
        }
        if(isset($data['price'])) {
            $record_update_main['price'] = $data['price'];
            unset($data['price']);
        } 
        if(isset($data['img_id'])) {
            $record_update_main['img_id'] = $data['img_id'];
            unset($data['img_id']);
        }
        if(isset($data['quantity'])) {
            $record_update_main['quantity'] = $data['quantity'];
            unset($data['quantity']);
        }
        if(isset($data['cat_id'])) {
            $record_update_main['cat_id'] = $data['cat_id'];
            unset($data['cat_id']);
        }
        if(!empty($record_update_main)) $this->db->update('ots_ticket',$record_update_main,array('id'=>$id_ticket));
        
        $get_news_lang = $this->get_details($id_ticket,true);
        if($get_news_lang) return $this->db->update('ticket_lang',$data,array('id_ticket'=>$id_ticket,'id_lang'=>$this->language));
        else {
            $data['id_ticket'] = $id_ticket;
            $data['id_lang'] = $this->language;
            return $this->db->insert('ticket_lang',$data)>0;
        }
        
        
    }
    function delete_ticket($id_ticket) {
        
        if (!empty($id_ticket)){
            $this->db->start_cache();
            $this->db->where('id_ticket', $id_ticket);
            $this->db->delete('ticket_lang');
            
            $this->db->stop_cache();
            $this->db->flush_cache();
            $this->db->where('ticket.id', $id_ticket);
            return $this->db->delete('ticket');
        }
            
    }
    
}

?>
